/**
  ******************************************************************************
  * @file 	MT9D111_stm_driver.h 
  * @brief  Headers for unit Tests
  ******************************************************************************
  */
#ifndef __MT9D111_H
#define __MT9D111_H
// lots of important defines here
#include "stm32f10x.h"
// dma is used for i2c transfers
#include "stm32f10x_dma.h"
// i2c is used to configure the camera
#include "stm32f10x_i2c.h"
// for configuring gpio, lots of it
#include "stm32f10x_gpio.h"
// for pwm output to drive camera
#include "stm32f10x_tim.h"
//
#include "stm32f10x_exti.h"
//
#include "stm32f10x_usart.h"
// 
#include "zbProtocol.h"

// other stuff ! 
#include "misc.h"
#include "stm32f10x_rcc.h"

// defines 
#define bufferSize 		4096		// size of the buffer of data we tx/rx
#define lineframevalid 	0x300 		// pins 8 and 9 set meaning frame and line valid
#define maxPackets		5			// max amount of packets we can have at a time, this is its own buffer X*(packetsize+overhead)
//#define packetSize 		1024		// size of one packet payload, no header
#define overhead 		16			// besides the actual data how much do we have in a packet ? 
#define id				0x77		// start of packet id

/*
  PACKET DEFINITION 

  ID 				1 BYTE
  PACKET TYPE 		1 BYTE
  PACKET NUMBER 	1 BYTE  - number of packet we are on
  TOTAL PACKETS 	1 BYTE 	- total packets in this sequence
  PACKET SIZE		2 BYTES
  MS UPTIME			4 BYTES - ms running or date time if time set
  CURRENT STATUS	4 BYTES
  DATA				? BYTES
  CRC				2 BYTES
*/

// errors, up to 16 stored in status, top 16 bits
#define ERR_SYSTICK 	0x80000000
#define ERR_PWM		 	ERR_SYSTICK/2
#define ERR_PKTOF		ERR_PWM/2 

// status up to 16, bottom half of status var
#define STAT_STARTUP 	0x1
#define STAT_STARTED	STAT_STARTUP*2

// packet type 
#define PACKET_INFO		0x00
#define PACKET_DATA		0x01

// #define true							!false
// #define false							0

// extern vars
extern u8 dataState;

// vars
extern u32 pwmTicks;
extern __IO uint32_t TimingDelay;
extern unsigned long pa0Ticks;
//extern u32 status;
extern float CCP2FQ;
static u8 txing;
//extern unsigned long DMATicks; // todo remove from extern
//extern float DMAFQ;

// function defines 
u8 testUnit(u8 unitNum);		// test unit #, returns error code

void initSystems(void);
void initCCP1(void);			// setup the compare capture for reading pclk
void initCCP2(void);			// capture compare for reading fq of other lines
void initPWM(float frequency);	// setup the pulse width modulation output
void initDMA1(void);			// setup DMA portE tx to memory buffer 
void initDMA2(void);			// setup DMA for usart tx of data
void NVIC_Configuration(void);	// setup interupts
void TIM1_UP_IRQHandler(void);	// int for timer1 update
void Delay(__IO uint32_t nTime);// delay function 
void initLed(void);				// led gpio setup
void initEXTI(void);			// int external int on toggle line 
void USART2_Config(void);		// config of usart2 which is the serial line
void USART3_Config(void);		// debug output
void DMA_TXPacket(u8 *packet, int len); // sends a packet out prepending header to beggining and using mem->usart dma
void DMA_RXPacket(u8 *packet, int len);
void BuildPacketsfromBuffer(void); 	// build all packets in packet buffer from data buffer so they are ready for sending
u32  getMSTick(void);					// return the current ticks
u32  getSecondTick(void);					// get the seconds, this could be uptime or unix time
void setSecondTick(u32 newSec);
void setLeds(u8 value);


#endif
